import QtQuick
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.0
import Qt.labs.platform 1.1

Window {
    objectName: "settingWindow"
    id: settingWindow
    width: 400
    height: 240
    y: (Screen.desktopAvailableHeight - 200) / 2
    x: (Screen.desktopAvailableWidth - 400) / 2
    visible: false
    flags: Qt.Window | Qt.FramelessWindowHint

    // 自定义标题栏
    Rectangle {
        id: titleBar
        height: 40
        color: "#252526"
        anchors {
            top: parent.top
            left: parent.left
            right: parent.right
        }

        Item {
            width: 39
            height: 24
            anchors.verticalCenter: parent.verticalCenter
            Image {
                source: "qrc:/image/res/screenshot.png"
                anchors.fill: parent
                anchors.leftMargin: 15
            }
        }

        // 标题文本
        Text {
            text: "设置"
            color: "white"
            font.pixelSize: 16
            anchors.verticalCenter: parent.verticalCenter
            leftPadding: 54
        }

        // 关闭按钮
        Button {
            id: closeButton
            width: 40
            height: 40
            anchors.right: parent.right
            background: Rectangle {
                color: closeButton.hovered ? "#E81123" : "transparent"
                Image {
                    source: "qrc:/image/res/close_0.png"  // 自定义关闭图标
                    anchors.centerIn: parent
                    width: 16; height: 16
                }
            }
            onClicked: settingWindow.hide()
            ToolTip {
                visible: parent.hovered
                text: "关闭"
                delay: 500
            }
        }

        // 窗口拖动区域
        MouseArea {
            anchors.fill: parent
            anchors.rightMargin: 40
            property point startPos: "0,0"
            onPressed: startPos = Qt.point(mouseX, mouseY)
            onPositionChanged: {
                if (pressed) {
                    var delta = Qt.point(mouseX - startPos.x, mouseY - startPos.y)
                    settingWindow.x += delta.x
                    settingWindow.y += delta.y
                }
            }
        }
    }

    Rectangle {
        border.width: 1
        border.color: "#515151"
        anchors.fill: parent
        anchors.topMargin: 39

        // 快捷键设置区域
        ColumnLayout {
            anchors {
                fill: parent
                leftMargin: 20
                rightMargin: 20
            }
            spacing: 15

            Label {
                text: "组合快捷键设置"
                color: "black"
                font.bold: true
            }

            // 快捷键输入框
            RowLayout {
                Label { text: "全屏截取:"; color: "black" }
                TextField {
                    id: shortcutFullScreenField
                    text: g_configHelper.readIniFile("FullScreen")
                    Layout.fillWidth: true
                    Keys.onPressed: event => {
                        // 捕获组合键
                        var keys = []
                        if (event.modifiers & Qt.ControlModifier) keys.push("Ctrl")
                        if (event.modifiers & Qt.AltModifier) keys.push("Alt")
                        if (event.modifiers & Qt.ShiftModifier) keys.push("Shift")
                        keys.push(getKeyName(event.key))
                        if (keys.length > 1)
                        {
                            text = keys.join("+")
                        }
                        event.accepted = true
                    }
                }
            }

            RowLayout {
                Label { text: "选区截取:"; color: "black" }
                TextField {
                    id: shortcutSelectionField
                    text: g_configHelper.readIniFile("Selection")
                    Layout.fillWidth: true
                    Keys.onPressed: event => {
                        // 捕获组合键
                        var keys = []
                        if (event.modifiers & Qt.ControlModifier) keys.push("Ctrl")
                        if (event.modifiers & Qt.AltModifier) keys.push("Alt")
                        if (event.modifiers & Qt.ShiftModifier) keys.push("Shift")
                        keys.push(getKeyName(event.key))
                        if (keys.length > 1)
                        {
                            text = keys.join("+")
                        }
                        event.accepted = true
                    }
                }
            }

            RowLayout {
                spacing: 50
                Item {
                    width: 60
                }
                Button {
                    text: "取消"
                    onClicked: settingWindow.hide()
                }

                Button {
                    text: "确认"
                    onClicked: {
                        // 写入配置文件
                        g_configHelper.writeIniFile("FullScreen", shortcutFullScreenField.text)
                        g_configHelper.writeIniFile("Selection", shortcutSelectionField.text)
                        // 更新全局热键
                        g_screenShot.updateHotKey()
                        settingWindow.hide()
                    }
                }
            }
        }
    }

    SystemTrayIcon {
        id:system_tray
        visible: true
        icon.source: "qrc:/image/res/favicon.ico"
        tooltip: "QScreenShot截图工具"
        menu: Menu {
            MenuItem {
                text: "全屏截取(" + g_configHelper.readIniFile("FullScreen") + ")"
                onTriggered: {

                }
            }
            MenuItem {
                text: "选区截取(" + g_configHelper.readIniFile("Selection") + ")"
                onTriggered: g_screenShot.selectionShot()
            }
            MenuSeparator {}
            MenuItem {
                text: "设置"
                onTriggered: {
                    popup.close()
                    settingWindow.show()
                }
            }
            MenuItem {
                text: "关于"
                onTriggered: {
                    settingWindow.show()
                    popup.open()
                }
            }
            MenuSeparator {}
            MenuItem {
                text: "退出"
                onTriggered: Qt.quit()
            }
        }
        onActivated:
            (reason)=>{
                if(reason === SystemTrayIcon.Trigger){
                    settingWindow.show()
                    settingWindow.raise()
                    settingWindow.requestActivate()
                }else if(reason === SystemTrayIcon.Context){
                    system_tray.menu.open()
                }
            }
    }

    // 弹窗组件
    Popup {
        id: popup
        width: 400
        height: 300
        modal: true
        focus: true
        anchors.centerIn: parent
        closePolicy: Popup.CloseOnEscape | Popup.CloseOnPressOutside

        background: Rectangle {
            color: "#f0f0f0"
            radius: 10
            border.color: "#cccccc"
        }

        // 使用行布局
        RowLayout {
            anchors.fill: parent
            anchors.margins: 20
            spacing: 30

            // 左侧：GIF图像
            AnimatedImage {
                id: gifImage
                Layout.preferredWidth: 174
                Layout.preferredHeight: 134
                source: "qrc:/image/res/tangzeng.gif"
                playing: true
                Layout.alignment: Qt.AlignVCenter
            }

            // 右侧：文字内容
            ColumnLayout {
                Layout.fillHeight: true
                Layout.alignment: Qt.AlignVCenter
                spacing: 15

                // 标题
                Label {
                    text: "关于QScreenshot"
                    font.bold: true
                    font.pixelSize: 20
                    Layout.alignment: Qt.AlignHCenter
                }

                // 版本信息
                RowLayout {
                    Layout.fillWidth: true
                    spacing: 5

                    Label {
                        text: "版本:"
                        font.bold: true
                        font.pixelSize: 16
                    }

                    Label {
                        text: "v0.2测试版"
                        font.pixelSize: 16
                        Layout.fillWidth: true
                    }
                }

                // 作者信息
                RowLayout {
                    Layout.fillWidth: true
                    spacing: 5

                    Label {
                        text: "作者:"
                        font.bold: true
                        font.pixelSize: 16
                    }

                    Label {
                        text: "唐僧洗头用飘柔"
                        font.pixelSize: 16
                        Layout.fillWidth: true
                    }
                }

                // 仓库地址
                RowLayout {
                    Layout.fillWidth: true
                    spacing: 5

                    Label {
                        text: "仓库:"
                        font.bold: true
                        font.pixelSize: 16
                    }

                    Text {
                        id: repoLink
                        property string link: "https://gitee.com/zzm3280/qscreenshot"
                        text: "<a href='https://gitee.com/zzm3280/qscreenshot'>访问代码仓库</a>"
                        font.pixelSize: 16
                        Layout.fillWidth: true
                        onLinkActivated: Qt.openUrlExternally(link)

                        MouseArea {
                            anchors.fill: parent
                            cursorShape: Qt.PointingHandCursor
                            acceptedButtons: Qt.NoButton
                        }
                    }
                }

                // 关闭按钮
                Button {
                    text: "关闭"
                    Layout.alignment: Qt.AlignHCenter
                    Layout.topMargin: 15
                    onClicked: {
                        settingWindow.hide()
                        popup.close()
                    }

                    background: Rectangle {
                        color: "#4a7bff"
                        radius: 5
                    }

                    contentItem: Text {
                        text: parent.text
                        color: "white"
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                    }
                }
            }
        }
    }

    // 键盘ascii与键名转换
    function getKeyName(key) {
        switch(key) {
            // 字母键
            case Qt.Key_A: return "A";
            case Qt.Key_B: return "B";
            case Qt.Key_C: return "C";
            case Qt.Key_D: return "D";
            case Qt.Key_E: return "E";
            case Qt.Key_F: return "F";
            case Qt.Key_G: return "G";
            case Qt.Key_H: return "H";
            case Qt.Key_I: return "I";
            case Qt.Key_J: return "J";
            case Qt.Key_K: return "K";
            case Qt.Key_L: return "L";
            case Qt.Key_M: return "M";
            case Qt.Key_N: return "N";
            case Qt.Key_O: return "O";
            case Qt.Key_P: return "P";
            case Qt.Key_Q: return "Q";
            case Qt.Key_R: return "R";
            case Qt.Key_S: return "S";
            case Qt.Key_T: return "T";
            case Qt.Key_U: return "U";
            case Qt.Key_V: return "V";
            case Qt.Key_W: return "W";
            case Qt.Key_X: return "X";
            case Qt.Key_Y: return "Y";
            case Qt.Key_Z: return "Z";

            // 数字键
            case Qt.Key_0: return "0";
            case Qt.Key_1: return "1";
            case Qt.Key_2: return "2";
            case Qt.Key_3: return "3";
            case Qt.Key_4: return "4";
            case Qt.Key_5: return "5";
            case Qt.Key_6: return "6";
            case Qt.Key_7: return "7";
            case Qt.Key_8: return "8";
            case Qt.Key_9: return "9";

            // 功能键
            case Qt.Key_F1: return "F1";
            case Qt.Key_F2: return "F2";
            case Qt.Key_F3: return "F3";
            case Qt.Key_F4: return "F4";
            case Qt.Key_F5: return "F5";
            case Qt.Key_F6: return "F6";
            case Qt.Key_F7: return "F7";
            case Qt.Key_F8: return "F8";
            case Qt.Key_F9: return "F9";
            case Qt.Key_F10: return "F10";
            case Qt.Key_F11: return "F11";
            case Qt.Key_F12: return "F12";

            // 方向键
            case Qt.Key_Up: return "↑";
            case Qt.Key_Down: return "↓";
            case Qt.Key_Left: return "←";
            case Qt.Key_Right: return "→";

            // 特殊键
            case Qt.Key_Enter: return "Enter";
            case Qt.Key_Return: return "Enter";
            case Qt.Key_Escape: return "Esc";
            case Qt.Key_Space: return "Space";
            case Qt.Key_Tab: return "Tab";
            case Qt.Key_Backspace: return "Backspace";
            case Qt.Key_Delete: return "Del";
            case Qt.Key_Insert: return "Ins";
            case Qt.Key_Home: return "Home";
            case Qt.Key_End: return "End";
            case Qt.Key_PageUp: return "PgUp";
            case Qt.Key_PageDown: return "PgDown";
            case Qt.Key_CapsLock: return "CapsLock";
            case Qt.Key_NumLock: return "NumLock";
            case Qt.Key_ScrollLock: return "ScrollLock";

            // 符号键
            case Qt.Key_Plus: return "+";
            case Qt.Key_Minus: return "-";
            case Qt.Key_Asterisk: return "*";
            case Qt.Key_Slash: return "/";
            case Qt.Key_Backslash: return "\\";
            case Qt.Key_Period: return ".";
            case Qt.Key_Comma: return ",";
            case Qt.Key_Colon: return ":";
            case Qt.Key_Semicolon: return ";";
            case Qt.Key_BracketLeft: return "[";
            case Qt.Key_BracketRight: return "]";
            case Qt.Key_BraceLeft: return "{";
            case Qt.Key_BraceRight: return "}";
            case Qt.Key_Less: return "<";
            case Qt.Key_Greater: return ">";
            case Qt.Key_Equal: return "=";
            case Qt.Key_QuoteDbl: return "\"";
            case Qt.Key_QuoteLeft: return "`";
            case Qt.Key_Question: return "?";
            case Qt.Key_Exclam: return "!";
            case Qt.Key_At: return "@";
            case Qt.Key_NumberSign: return "#";
            case Qt.Key_Dollar: return "$";
            case Qt.Key_Percent: return "%";
            case Qt.Key_AsciiCircum: return "^";
            case Qt.Key_Ampersand: return "&";
            case Qt.Key_ParenLeft: return "(";
            case Qt.Key_ParenRight: return ")";
            case Qt.Key_Underscore: return "_";

            // 媒体键
            case Qt.Key_VolumeDown: return "VolDown";
            case Qt.Key_VolumeUp: return "VolUp";
            case Qt.Key_VolumeMute: return "Mute";
            case Qt.Key_MediaPlay: return "Play";
            case Qt.Key_MediaStop: return "Stop";
            case Qt.Key_MediaPrevious: return "Prev";
            case Qt.Key_MediaNext: return "Next";

            // 小键盘键
            case Qt.Key_Keypad0: return "Num0";
            case Qt.Key_Keypad1: return "Num1";
            case Qt.Key_Keypad2: return "Num2";
            case Qt.Key_Keypad3: return "Num3";
            case Qt.Key_Keypad4: return "Num4";
            case Qt.Key_Keypad5: return "Num5";
            case Qt.Key_Keypad6: return "Num6";
            case Qt.Key_Keypad7: return "Num7";
            case Qt.Key_Keypad8: return "Num8";
            case Qt.Key_Keypad9: return "Num9";
            case Qt.Key_KeypadDecimal: return "Num.";
            case Qt.Key_KeypadMultiply: return "Num*";
            case Qt.Key_KeypadAdd: return "Num+";
            case Qt.Key_KeypadSubtract: return "Num-";
            case Qt.Key_KeypadDivide: return "Num/";
            case Qt.Key_KeypadEnter: return "NumEnter";

            // 其他特殊键
            case Qt.Key_Shift: return "Shift";
            case Qt.Key_Control: return "Ctrl";
            case Qt.Key_Alt: return "Alt";
            case Qt.Key_Meta: return "Meta";
            case Qt.Key_Menu: return "Menu";
            case Qt.Key_Help: return "Help";
            case Qt.Key_Print: return "Print";
            case Qt.Key_SysReq: return "SysReq";
            case Qt.Key_Pause: return "Pause";
            case Qt.Key_Cancel: return "Cancel";

            default:
                // 尝试转换为字符（适用于ASCII字符）
                if (key >= Qt.Key_Space && key <= Qt.Key_AsciiTilde) {
                    return String.fromCharCode(key);
                }
                return "";
        }
    }
}
